﻿Public Class EczaneData : Inherits Process

	Dim Doc As New Xml.XmlDocument

	Public Overrides Sub GenericEvent(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Eczane datası oluştur", "")
			Case EventType.Shown
				Buttons.Show("Save")
			Case EventType.Save
				Dim tables = AddElement(Doc, "tables")
				Dim sgenelall = AddElement(tables, "sgenel")
				Rs = Remote.OpenSQL("select sno,kimlik,adi,soyadi,dtar from sgenel order by kimlik")
				For Each Row In Rs
					Dim sgenel = AddElement(sgenelall, "data")
					AddElement(sgenel, "sno", Trim(Rs("sno")))
					AddElement(sgenel, "kimlik", Trim(Rs("kimlik")))
					AddElement(sgenel, "adi", Trim(Rs("adi")))
					AddElement(sgenel, "soyadi", Trim(Rs("soyadi")))
					AddElement(sgenel, "dtar", YMD(Rs("dtar")))
				Next
				Dim ilacall = AddElement(tables, "ilac")
				Q("select a.barkod,b.ilactadi,c.fiat")
				Q("from ssdmerkez..ilacbarkod a inner join ssdmerkez..ilactisim b on a.ilactkod = b.ilactkod inner join ssdmerkez..ilacfiat c on c.ilactkod = b.ilactkod")
				Q("where not exists(select * from ssdmerkez..ilacfiat d where c.ilactkod = d.ilactkod and d.tarih > c.tarih)")
				Rs = Remote.OpenSQL("*")
				For Each Row In Rs
					Dim ilac = AddElement(ilacall, "ilac")
					AddElement(ilac, "barkod", Trim(Rs("barkod")))
					AddElement(ilac, "ilactadi", Trim(Rs("ilactadi")))
					AddElement(ilac, "fiat", Trim(Rs("fiat")))
				Next
				Save(Doc.InnerXml, "001.txt")
				Info("Veri dosyası oluşturuldu ve sisteme yüklendi.")
				Close()
		End Select
	End Sub

	Function AddElement(Parent As Xml.XmlNode, Text As String) As Xml.XmlNode
		Dim Node = Doc.CreateNode(Xml.XmlNodeType.Element, Text, Doc.NamespaceURI)
		Parent.AppendChild(Node)
		Return Node
	End Function

	Function AddElement(Parent As Xml.XmlNode, Text As String, Value As String) As Xml.XmlNode
		Dim Node = Doc.CreateNode(Xml.XmlNodeType.Attribute, Text, Doc.NamespaceURI)
		Node.Value = Value
		Parent.Attributes.Append(Node)
		Return Node
	End Function

	Private Sub Save(Text As String, File As String)
		Dim Encoding As New System.Text.UTF8Encoding()
		Dim Bytes = Encoding.GetBytes(Text)
		Dim MemoryStream As New IO.MemoryStream
		Dim Zipper As New System.IO.Compression.GZipStream(MemoryStream, IO.Compression.CompressionMode.Compress)
		Zipper.Write(Bytes, 0, Bytes.Length)
		Zipper.Close()
		My.Computer.FileSystem.WriteAllBytes("\\192.168.100.17\wwwroot\default\eczane\" & File, MemoryStream.ToArray, False)
	End Sub

End Class
